from django.contrib import admin
from django.contrib import admin
from django.utils.html import format_html

from .models import Subject, Course, Module


# Register your models here.


@admin.register(Subject)
class SubjectAdmin(admin.ModelAdmin):
    list_display = ['title', 'slug', 'operator']
    prepopulated_fields = {'slug': ('title',)}

    def operator(self, obj):
        return format_html(f'<a href="/admin/courses/subject/{obj.id}/change/">编辑</a>')

    operator.short_description = '操作'


class ModuleInline(admin.StackedInline):
    model = Module


@admin.register(Course)
class CourseAdmin(admin.ModelAdmin):
    list_display = ['title', 'subject', 'created', 'operator']
    list_filter = ['created', 'subject']
    search_fields = ['title', 'overview']
    prepopulated_fields = {'slug': ('title',)}
    inlines = [ModuleInline]

    def operator(self, obj):
        return format_html(f'<a href="/admin/courses/course/{obj.id}/change/">编辑</a>')

    operator.short_description = '操作'


admin.site.site_header = '课程管理'
admin.site.site_title = '课程管理后台'
